package edu.fudan.jwxt.controller;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.common.ResultBody;
import edu.fudan.jwxt.dto.CourseDto;
import edu.fudan.jwxt.entity.TeacherCourse;
import edu.fudan.jwxt.entity.User;
import edu.fudan.jwxt.entity.VO.StudentCourseVO;
import edu.fudan.jwxt.service.CourseService;
import edu.fudan.jwxt.service.StudentCourseVOService;
import edu.fudan.jwxt.service.TeacherCourseService;
import edu.fudan.jwxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    StudentCourseVOService studentCourseVOService;

    @Autowired
    UserService userService;

    @Autowired
    TeacherCourseService teacherCourseService;

    @Autowired
    CourseService courseService;

    //TODO：CourseDto的填充
    @GetMapping("/getcourse/{uid}")
    public ResultBody getCourse(@PathVariable String uid){
        LambdaQueryWrapper<StudentCourseVO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentCourseVO::getUid,uid);
        List<StudentCourseVO> studentCourseVOList = studentCourseVOService.list(queryWrapper);

        List<CourseDto> courseList = new ArrayList<>();
//        for (StudentCourseVO studentCourseVO : studentCourseVOList){
//            courseList.add(courseService.get)
//        }
        JSONArray jsonArray = new JSONArray(courseList);

        return ResultBody.success(jsonArray);
    }

    @PostMapping("/addcourse")
    public ResultBody addCourse(@RequestBody StudentCourseVO studentCourseVO){
        if (studentCourseVO.getUid() == null || studentCourseVO.getCourseId() == null)
            return ResultBody.error(Constants.CODE_600,"学号或课程不能为空");

        User user = userService.getById(studentCourseVO.getUid());
        if(user == null)
            return ResultBody.error(Constants.CODE_600,"该学生不存在");
        LambdaQueryWrapper<TeacherCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeacherCourse::getCourseId,studentCourseVO.getCourseId())
                        .eq(TeacherCourse::getClassId,studentCourseVO.getClassId());
        TeacherCourse one = teacherCourseService.getOne(queryWrapper);
        if(one == null)
            return ResultBody.error(Constants.CODE_600,"不存在该课程");

        studentCourseVOService.save(studentCourseVO);
        return ResultBody.success();
    }

    @PostMapping("/deletecourse")
    public ResultBody deleteCourse(@RequestBody StudentCourseVO studentCourseVO){
        if (studentCourseVO.getUid() == null || studentCourseVO.getCourseId() == null)
            return ResultBody.error(Constants.CODE_600,"学号或课程不能为空");

        User user = userService.getById(studentCourseVO.getUid());
        if(user == null)
            return ResultBody.error(Constants.CODE_600,"该学生不存在");
        LambdaQueryWrapper<TeacherCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeacherCourse::getCourseId,studentCourseVO.getCourseId())
                .eq(TeacherCourse::getClassId,studentCourseVO.getClassId());
        TeacherCourse one = teacherCourseService.getOne(queryWrapper);
        if(one == null)
            return ResultBody.error(Constants.CODE_600,"不存在该课程");

        LambdaQueryWrapper<StudentCourseVO> qw = new LambdaQueryWrapper<>();
        qw.eq(StudentCourseVO::getUid,studentCourseVO.getUid())
                        .eq(StudentCourseVO::getCourseId,studentCourseVO.getCourseId())
                                .eq(StudentCourseVO::getClassId,studentCourseVO.getClassId());

        studentCourseVOService.remove(qw);
        return ResultBody.success();
    }


}
